// INCLUDES --------------------------------------------------------------------------
#include "Framework.h"

// FUNCTIONS -------------------------------------------------------------------------

// event callbacks: window reshape, window status update, keyboard/mouse input

// --> window reshape
void Window(i32 width, i32 height)
{
  // TODO: needs to use CAMERA!!!

  // setup the viewport
  RenderViewport((ui32)width, (ui32)height);
};

// --> window status update
void WindowStatus(i32 state)
{
  // register state of the Compute() function
  static bool computeRegistered = true;

  // check for window status
  switch (state)
  {
#ifdef GLUT
    case GLUT_HIDDEN:
    case GLUT_FULLY_COVERED:
      // window is not properly visible.
      // un-register idle callback and therefor stop rendering!

      // un-register Compute()
      glutIdleFunc(NULL);
#endif // GLUT
      computeRegistered = false;
      break;

    // default: check if rendering needs to be picked up
    default:
    {
      // check if we need to initiate the computation callback
      if (computeRegistered == false)
      {
#ifdef GLUT
        // re-register Compute()
        glutIdleFunc(Compute);
#endif // GLUT
        computeRegistered = true;
      }
      break;
    }
  };
};

// --> keyboard input
void Keyboard(ub8 key, i32 x, i32 y)
{
};

// --> mouse input
void Mouse(i32 button, i32 state, i32 x, i32 y)
{
};

// -----------------------------------------------------------------------------------

// init basic window functionality
void WindowInit(i32 argc, b8 *argv[])
{
#ifdef GLUT
  // init basics
  glutInit(&argc, (b8**)argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
  glutInitWindowSize(640, 480);
  glutInitWindowPosition(10, 10);
#endif // GLUT
}

// show properly inited window
void WindowShow()
{
#ifdef GLUT
  // create the window
  glutCreateWindow("");

  // register function callbacks
  glutReshapeFunc     (Window);       // event -> window reshape
  glutWindowStatusFunc(WindowStatus); // event -> window status update

  glutKeyboardFunc    (Keyboard);     // event -> keyboard input
  glutMouseFunc       (Mouse);        // event -> mouse input

  glutIdleFunc        (Compute);      // idle callback
  glutDisplayFunc     (Render);       // render callback

  // enter main processing loop
  glutMainLoop();
#endif // GLUT
};

// set title of the window
void WindowSetTitle(b8 *str)
{
#ifdef GLUT
  glutSetWindowTitle(str);
#endif // GLUT
};

// mark screen as "needs to be redrawn"
void WindowRedisplay()
{
#ifdef GLUT
  glutPostRedisplay(); // invalidate current screen
#endif // GLUT
};

// swap back-/frontbuffer
void WindowSwapBuffers()
{
#ifdef GLUT
  glutSwapBuffers(); // tell window manager to swap buffers
#endif // GLUT
};
